import java.io.*;
import java.util.Scanner;

public class FileCopy {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 获取源文件和目标文件路径
        System.out.print("请输入源文件路径: ");
        String sourcePath = scanner.nextLine();
        System.out.print("请输入目标文件路径: ");
        String destinationPath = scanner.nextLine();

        File sourceFile = new File(sourcePath);

        // 检查源文件是否存在
        if (!sourceFile.exists()) {
            System.out.println("源文件不存在！");
            scanner.close();
            return;
        }

        // 根据文件扩展名判断文件类型
        boolean isTextFile = sourcePath.toLowerCase().endsWith(".txt");

        try {
            long startTime = System.currentTimeMillis();

            if (isTextFile) {
                copyTextFile(sourcePath, destinationPath, sourceFile);
            } else {
                copyBinaryFile(sourcePath, destinationPath, sourceFile);
            }

            long endTime = System.currentTimeMillis();
            System.out.println("\n复制完成，耗时 " + (endTime - startTime) + " 毫秒");

            // 如果是文本文件，读取并打印内容
            if (isTextFile) {
                System.out.println("\n源文件内容：");
                readTextFile(sourcePath);
            }

        } catch (IOException e) {
            System.out.println("错误: " + e.getMessage());
        }

        scanner.close();
    }

    private static void copyTextFile(String sourcePath, String destinationPath, File sourceFile) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(sourcePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(destinationPath))) {

            long totalSize = sourceFile.length();
            long copiedSize = 0;
            String line;
            int lineCount = 0;

            // 计算总行数用于进度计算
            try (LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(sourcePath))) {
                while (lineNumberReader.skip(Long.MAX_VALUE) > 0) {
                }
                lineCount = lineNumberReader.getLineNumber() + 1;
            }

            int currentLine = 0;
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();
                copiedSize += line.length() + System.lineSeparator().length();
                currentLine++;

                // 计算并显示进度
                int progress = (int) ((currentLine * 100.0) / lineCount);
                System.out.print("\r复制进度: " + progress + "%");
            }
        }
    }

    private static void copyBinaryFile(String sourcePath, String destinationPath, File sourceFile) throws IOException {
        try (FileInputStream input = new FileInputStream(sourcePath);
             FileOutputStream output = new FileOutputStream(destinationPath)) {

            long totalSize = sourceFile.length();
            long copiedSize = 0;
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = input.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
                copiedSize += bytesRead;

                // 计算并显示进度
                int progress = (int) ((copiedSize * 100.0) / totalSize);
                System.out.print("\r复制进度: " + progress + "%");
            }
        }
    }

    private static void readTextFile(String filePath) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        }
    }
}